package cn.kgc.mes.storage.business;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.kgc.mes.common.utils.StringUtils;
import org.springframework.stereotype.Component;
import cn.kgc.mes.common.core.plus.dto.common.CommonDataPageResponse;
import cn.kgc.mes.common.core.plus.dto.page.PageDtoRequest;
import cn.kgc.mes.common.core.plus.dto.page.PageDtoResponse;
import cn.kgc.mes.common.core.plus.response.StatusResponse;
import org.springframework.beans.BeanUtils;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import cn.kgc.mes.storage.domain.vo.WmItemRecptVo;
import cn.kgc.mes.storage.domain.WmItemRecpt;
import cn.kgc.mes.storage.service.WmItemRecptService;

/**
 * 物料入库单业务
 * 
 * @author cly‘
 * @date 2025-09-14
 */
@Component
public class WmItemRecptBusiness {

    @Resource
    WmItemRecptService settingService;


    /**
     * 分页
     *
     * @param vo
     * @return
     */
    public CommonDataPageResponse<WmItemRecptVo> page(WmItemRecptVo vo) {
        CommonDataPageResponse<WmItemRecptVo> response = new CommonDataPageResponse<>();

        //分页
        Long current = 1L;
        Long size = 10L;

        PageDtoRequest reqPageDto = vo.getPageDto();
        if (reqPageDto != null) {
            current = reqPageDto.getCurrent() != null ? reqPageDto.getCurrent() : 1L;
            size = reqPageDto.getSize() != null ? reqPageDto.getSize() : 10L;
        }

        Page page = new Page<>(current, size);

        LambdaQueryWrapper<WmItemRecpt> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper.eq(StringUtils.hasText(vo.getRecptCode()), WmItemRecpt::getRecptCode, vo.getRecptCode());

        queryWrapper.like(StringUtils.hasText(vo.getRecptName()), WmItemRecpt::getRecptName, vo.getRecptName());

        queryWrapper.like(StringUtils.hasText(vo.getVendorName()), WmItemRecpt::getVendorName, vo.getVendorName());

        queryWrapper.eq(vo.getRecptDate() != null, WmItemRecpt::getRecptDate, vo.getRecptDate());

        queryWrapper.eq(StringUtils.hasText(vo.getStatus()), WmItemRecpt::getStatus, vo.getStatus());

        queryWrapper.orderByDesc(WmItemRecpt::getRecptId);

        IPage<WmItemRecpt> pageRecords = settingService.page(page, queryWrapper);

        //分页记录
        List<WmItemRecpt> records = pageRecords.getRecords();
        //转为vo
        List<WmItemRecptVo> voList = new ArrayList<>();
        for (WmItemRecpt record : records) {
            WmItemRecptVo settingVo  = new WmItemRecptVo();

            BeanUtils.copyProperties(record, settingVo);

            voList.add(settingVo);
        }
        response.setRecords(voList);

        //分页信息
        PageDtoResponse pageDto = new PageDtoResponse();
        pageDto.setCurrent(current);
        pageDto.setSize(size);
        pageDto.setTotal(pageRecords.getTotal());
        pageDto.setPages(pageRecords.getPages());

        response.setPageDto(pageDto);

        return response;
    }

    /**
     * 信息
     *
     * @param vo
     * @return
     */
    public WmItemRecptVo info(WmItemRecptVo vo) {

        WmItemRecptVo settingVo = new WmItemRecptVo();

        Long recptId = vo.getRecptId();

        WmItemRecpt record = settingService.getById(recptId);

        BeanUtils.copyProperties(record, settingVo);

        return settingVo;
    }

    /**
     * 添加
     *
     * @param vo
     * @return
     */
    public StatusResponse add(WmItemRecptVo vo) {
        StatusResponse response = new StatusResponse();

        WmItemRecpt record = new WmItemRecpt();

        BeanUtils.copyProperties(vo, record);

        settingService.save(record);

        response.setSuccess(true);

        return response;
    }

    /**
     * 更新
     *
     * @param vo
     * @return
     */
    public StatusResponse update(WmItemRecptVo vo) {
        StatusResponse response = new StatusResponse();

        WmItemRecpt record = new WmItemRecpt();

        BeanUtils.copyProperties(vo, record);

        settingService.updateById(record);

        response.setSuccess(true);

        return response;
    }

    /**
     * 删除
     *
     * @param vo
     * @return
     */
    public StatusResponse delete(WmItemRecptVo vo) {
        StatusResponse response = new StatusResponse();

        Long recptId = vo.getRecptId();

        settingService.removeById(recptId);

        response.setSuccess(true);

        return response;
    }

}
